// 章
const {
    getChapterById,   // 根据id 查询章
    getChapterList,   //  查询章列表
    createChapter,   // 添加章
    editChapter,   // 编辑章
    removeChapter,   // 删除章
} = require("../../service/catalogue/chapter")

exports.chapterApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/")[1]
    const { id } = ctx.params || "";
    const { title, catalogue_id, updator, creator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (ctx.request.url.length > url.length + 1) {
            res = await getChapterById({ id })
        } else {
            res = await getChapterList()
        }; break;
        case "POST": res = await createChapter({ title, creator, catalogue_id }); break;
        case "DELETE": res = await removeChapter({ id }); break;
        case "PUT": res = await editChapter({ id, title, updator, catalogue_id }); break;
        default: res = {msg:"操作失败", code:"400"}
    }
    ctx.body = res
}